package com.kadron.registration;

import java.io.Serializable;
import java.util.Date;

import javax.transaction.UserTransaction;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.international.StatusMessage;

import com.kadron.common.KConst;
import com.kadron.common.KUtil;
import com.kadron.common.PasswordUtil;
import com.kadron.dao.KUserDAO;
import com.kadron.entity.User;
import com.kadron.enumeration.Gender;
import com.kadron.enumeration.KLang;
import com.kadron.enumeration.UserType;
import com.kadron.mail.KMailService;
import com.kadron.manager.KMessageManager;
import com.kadron.service.CommonUserService;
import com.kadron.service.UserHistoryService;

@AutoCreate
@Name("kRegistration")
@Scope(ScopeType.PAGE)
public class KRegistration extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5307825474425830753L;
	
	@In
	private KUserDAO userDAO;
	
	@In
	private UserHistoryService userHistoryService;
	
	private String username;
	private String email;
	private String password;
	private String confirmPassword;
	private String nickname;
	private String firstname;
	private String lastname;
	private String address;
	
	private String tel;
	private String mob;
	
	private String avatar;
	
	private Date birthday;
	private KLang lang;
	
	private String desc;
	
	private boolean newsletter;
	
	private boolean nameVisible = true;
	private boolean emailVisible = true;
	private boolean birthdayVisible = true;
	private boolean telVisible = true;
	
	private boolean acceptTerms = true;
	
	private Gender gender = Gender.MALE;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getMob() {
		return mob;
	}

	public void setMob(String mob) {
		this.mob = mob;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public KLang getLang() {
		return lang;
	}

	public void setLang(KLang lang) {
		this.lang = lang;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public boolean isNewsletter() {
		return newsletter;
	}

	public void setNewsletter(boolean newsletter) {
		this.newsletter = newsletter;
	}

	public boolean isNameVisible() {
		return nameVisible;
	}

	public void setNameVisible(boolean nameVisible) {
		this.nameVisible = nameVisible;
	}

	public boolean isEmailVisible() {
		return emailVisible;
	}

	public void setEmailVisible(boolean emailVisible) {
		this.emailVisible = emailVisible;
	}

	public boolean isBirthdayVisible() {
		return birthdayVisible;
	}

	public void setBirthdayVisible(boolean birthdayVisible) {
		this.birthdayVisible = birthdayVisible;
	}

	public boolean isTelVisible() {
		return telVisible;
	}

	public void setTelVisible(boolean telVisible) {
		this.telVisible = telVisible;
	}

	public boolean isAcceptTerms() {
		return acceptTerms;
	}

	public void setAcceptTerms(boolean acceptTerms) {
		this.acceptTerms = acceptTerms;
	}
	
	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public String save() {
		if (!acceptTerms  
				|| KUtil.isEmpty(password) 
				|| KUtil.isEmpty(confirmPassword) 
				|| !password.equals(confirmPassword)
				|| KUtil.isEmpty(firstname)
				|| KUtil.isEmpty(lastname)
				|| KUtil.isEmpty(tel)
				|| KUtil.isEmpty(mob)
				|| birthday == null
				|| gender == null
				|| KUtil.isEmpty(email)) {
			bad("field_values_are_not_correct");
			return null;
		}
		boolean exists = userDAO.check(username, email);
		if (exists) {
			message("user_exist_msg", StatusMessage.Severity.WARN);
			return null;
		}
		UserTransaction tx = org.jboss.seam.transaction.Transaction.instance();
		try {
//			String password = KIDGenerator.generateString(8);
			String criptedPassword = PasswordUtil.cript(password);
			User user = new User(username, criptedPassword, email, nickname, firstname, lastname, address, tel, mob, birthday, lang, desc, newsletter, nameVisible, emailVisible, birthdayVisible, telVisible, gender);
			user.setAcceptedTerms(true);
			long ordinal = CommonUserService.getCounter();
			user.setOrdinal(ordinal);
			if (ordinal < 10) {
				user.setUserType(UserType.STANDARD_FIST_BUY);
			} else if (exists) {
				user.setUserType(UserType.STANDARD_10);			
			}
			user = (User) userDAO.update(user);
			KMailService.sendReg(password, user);
			userHistoryService.create(user, KConst.REGISTRATION, KConst.REGISTRATION, "User K-NAME="+user.getUsername()+" registered as UserType="+user.getUserType()+", user ordinal="+ordinal);
			tx.commit();
			good();
			return KConst.HOME;
		} catch (Exception e) {
			try {
				tx.rollback();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		bad(KConst.BAD_THING);
		return null;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
	
}
